﻿//***********************************************************************
//* Module:  TbBstUserDAL
// * 作者:  曹荣芝
//* Modified: 2013-03-25
// * Purpose: Definition of the Class TbBstUserDAL
// ***********************************************************************/
using System;
using System.Data;
using XWG.JWBST;
using XWG.JWBST.JWBSTEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthUtility;
using XWG.JWBST.JWBSTSPConst;
using XWG;
using XWG.Framework.MSException;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data.OracleClient;
namespace XWG.JWBST.JWBSTDAL
{
    /// <summary>
    /// 
    /// </summary>
    public class TbBstUserDAL
    {
        #region 删除操作
        /// <summary>
        /// 删除。
        /// </summary>
        /// <param name="userid">要删除的的主键值。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>是否成功 1 表示成功 负值代表失败</returns>
        public static ResultModel Delete(object id, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();//定义错误信息类
            try
            {
                //载入实例
                JWBSTEntity.TbBstUser u = JWBSTDAL.TbBstUserDAL.Load(id) as JWBSTEntity.TbBstUser;

                if (isLog)//如果写入日志
                {
                    //    AppLog log = new AppLog();
                    //    log.UserId = Convert.ToString(userid);
                    //    log.Id = System.Guid.NewGuid().ToString();
                    //    log.LogContent = "删除";
                    //    log.LogObject = TbBstUserConst.TableName;
                    //    log.LogType = "操作日志";
                    //    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    //    Db.TransDelete<TbBstUser, AppLog>(u, log);
                }
                else//不写入日志
                {
                    u.Deltag = 1;
                    u.Deltime = DateTime.Now;
                    Db.TransUpdate(u);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = Convert.ToString(userid);
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {

                resultModel.ErrorCode = -9994;
                resultModel.FiledKey = Convert.ToString(userid);
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的删除操作");
                return resultModel;

            }
        }
        #endregion

        #region 根据主键查询一条记录
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="userid">主键</param>
        /// <returns>用户实体</returns>
        public static JWBSTEntity.TbBstUser Load(object userid)
        {
            return Db.GetModel<JWBSTEntity.TbBstUser>(userid);
        }
        #endregion

        #region 查询操作
        /// <summary>
        /// 查询
        /// </summary>
        public static IList<TbBstUser> SelectList()
        {
            try
            {
                ICriteria crt = Db.Session.CreateCriteria(typeof(TbBstUser));
                // crt.Add(NHibernate.Criterion.Expression.Eq(TbBstUserConst.Columns.EnabledPropertyName, Enabled.TRUE));
                IList<TbBstUser> list = crt.List<TbBstUser>();
                return list;
            }
            catch (Exception ex)
            {
                XWG.Framework.MSException.MSEx.DealErr(ex, "查询");
                return null;
            }
            finally
            {

            }
        }
        #endregion
        #region 插入操作
        /// <summary>
        /// 新增。
        /// </summary>
        /// <param name="info">待新增的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>新增是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Insert(JWBSTEntity.TbBstUser info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            object filedkey;
            try
            {
                if (isLog)
                {

                    //AppLog log = new AppLog();
                    //log.UserId = Convert.ToString(userid);
                    //log.Id = System.Guid.NewGuid().ToString();
                    //log.LogContent = "新建";
                    //log.LogObject = TbBstUserConst.TableName;
                    //log.LogType = "操作日志";
                    //log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    //filedkey= Db.TransInsert<TbBstUser,AppLog>(info,log);
                }
                else
                {
                    filedkey = Db.TransInsert<TbBstUser>(info);
                }
                resultModel.ErrorCode = 0;
                //resultModel.FiledKey = Convert.ToString(filedkey);
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {
                resultModel.ErrorCode = -9998;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的插入操作");
                return resultModel;
            }
        }
        #endregion

        #region 修改操作
        /// <summary>
        /// 修改。
        /// </summary>
        /// <param name="info">待修改的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>修改是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Update(JWBSTEntity.TbBstUser info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                if (isLog)
                {
                    //AppLog log = new AppLog();
                    //log.UserId = Convert.ToString(userid);
                    //log.Id = System.Guid.NewGuid().ToString();
                    //log.LogContent = "修改";
                    //log.LogObject = TbBstUserConst.TableName;
                    //log.LogType = "操作日志";
                    //log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    //Db.TransUpdate<TbBstUser, AppLog>(info, log);
                }
                else
                {
                    Db.TransUpdate<TbBstUser>(info);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {
                resultModel.ErrorCode = -9997;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的修改操作");
                return resultModel;
            }
        }

        /// <summary>
        /// 查询此电话号码是否注册
        /// </summary>
        /// <param name="PhoneNo">手机号</param>
        /// <returns>i  i = 0可以注册  i > 0 此手机号已经注册</returns>
        public static int SelectUser(string PhoneNo)
        {
            string sql = "select count(*) from TB_BST_USER where PHONE_NO = :PHONE_NO";
            OracleParameter[] sp = { 
                                   new OracleParameter(":PHONE_NO",PhoneNo)
                                   };
            int i = Convert.ToInt32(OrclHelper.ExecuteScalar(sql, sp));
            return i;
        }
        #endregion

        #region 用户登录
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="phoneon">手机号</param>
        /// <param name="password">密码</param>
        /// <returns>用户的ID</returns>
        public static string UserLogin(string phoneon, string password)
        {
            string sql = "select user_id from tb_bst_user where phone_no = :phoneon and pass_word = :password ";
            OracleParameter[] sp = {
                                   new OracleParameter(":phoneon",phoneon),
                                   new OracleParameter(":password",password)
                                   };
            return OrclHelper.ExecuteScalar(sql, sp);
        }
        #endregion

        #region
        /// <summary>
        /// 更新最后登陆时间
        /// </summary>
        /// <param name="uid">用户ID</param>
        public static void Updatetime(string uid)
        {
            string sql = "update tb_bst_user set last_login_date = :lastlogintime where user_id = :userid";
            OracleParameter[] sp = {
                                   new OracleParameter(":lastlogintime",DateTime.Now),
                                   new OracleParameter(":userid",uid)
                                   };
            OrclHelper.ExecuteOracle(sql, sp);
        }
        #endregion

        //#region
        ///// <summary>
        ///// 获取用户ID
        ///// </summary>
        ///// <param name="phone">手机号</param>
        //public static string GetUserID(string phone)
        //{
        //    string sql = "select user_id from tb_bst_user where phone_no = :phone";
        //    OracleParameter[] sp = {
        //                             new OracleParameter(":phone",phone)
        //                           };
        //   return OrclHelper.ExecuteScalar(sql, sp);
        //}
        //#endregion

        /// <summary>
        /// 验证用户是否存在
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static string ISExist(string phone,string pass)
        {
            string sql = "select * from TB_BST_USER where phone_no = :phone and pass_word = :pass";
            OracleParameter[] sp = {
                                         new OracleParameter(":phone",phone),
                                         new OracleParameter(":pass",pass),
                                       };
            return OrclHelper.ExecuteScalar(sql, sp).ToString();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="pass"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static int ModefiyPass(string pass,string uid)
        {
            string sql = "update TB_BST_USER set pass_word = :pass where user_id =:uids";
            OracleParameter[] sp = {
                                         new OracleParameter(":pass",pass),
                                         new OracleParameter(":uids",uid)
                                       };
            return OrclHelper.ExecuteOracle(sql, sp);
        }
    }
}
